/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Main;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Admin
 */
public class frmChonDeThi extends javax.swing.JFrame {

    /**
     * Creates new form frmMain
     */
    public frmChonDeThi() {
        initComponents();
      layDSDeThi();
      loadDSDeDaThi();
    }
//Danh sach de thi
    List<DeThi> dsDeThi = new ArrayList<DeThi>();

    //Lay danh sach de thi
    private void layDSDeThi() {
        //Lay danh sach de thi theo username
        dsDeThi = DeThi.layDanhSachDeThi();
        //Tao model cho jtable
        DefaultTableModel model = new DefaultTableModel();
        //Tao cot cho model
        model.addColumn("Mã đề");
        model.addColumn("Tiêu đề");

        //Them dong cho model
        for (int i = 0; i < dsDeThi.size(); i++) {
            DeThi dt = dsDeThi.get(i);
            Vector v = new Vector();
            v.add(dt.getMaDe());
            v.add(dt.getTieuDe());
            model.addRow(v);
        }

        //Set model cho jtable
        tblDSDeThi.setModel(model);
    }
    
    //Lay danh sach ca de thi da thi va diem so
    private void loadDSDeDaThi(){
        List<DiemThi> dsDiemThi=new ArrayList<DiemThi>();
        dsDiemThi=DiemThi.layDSDiemThiTheoUsername(Main.nguoiDungDangNhap.getUserName());
        DefaultTableModel model=new DefaultTableModel();
        model.addColumn("Đề thi");
        model.addColumn("Điểm số");
        model.addColumn("Ngày thi");
        for(int i=0;i<dsDiemThi.size();i++){
            DiemThi dt=dsDiemThi.get(i);
            DeThi deThi=DeThi.layDeThiTheoID(dt.getMaDeThi());
            Vector v=new Vector();
            v.add(deThi.getTieuDe());
            v.add(dt.getDiem());
            v.add(dt.getNgayThi());
            
            model.addRow(v);
        }
        
        tblDeDaThi.setModel(model);
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblDSDeThi = new javax.swing.JTable();
        btnBatDauThi = new javax.swing.JButton();
        btnThoat = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblDeDaThi = new javax.swing.JTable();

        jButton1.setText("jButton1");

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setText("DANH SÁCH ĐỀ THI");

        jScrollPane2.setViewportView(tblDSDeThi);

        btnBatDauThi.setText("Bắt đầu thi");
        btnBatDauThi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBatDauThiActionPerformed(evt);
            }
        });

        btnThoat.setText("Thoát");
        btnThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThoatActionPerformed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel2.setText("CÁC ĐỀ ĐÃ THI");

        jScrollPane1.setViewportView(tblDeDaThi);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .addContainerGap()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 336, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(layout.createSequentialGroup()
                                .add(btnBatDauThi)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(btnThoat)))
                        .add(18, 18, 18)
                        .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 241, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(layout.createSequentialGroup()
                        .add(110, 110, 110)
                        .add(jLabel1)
                        .add(177, 177, 177)
                        .add(jLabel2)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(12, 12, 12)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(jLabel2))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(btnBatDauThi)
                    .add(btnThoat))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 287, Short.MAX_VALUE))
                .addContainerGap(33, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThoatActionPerformed
        // TODO add your handling code here:
        frmLogin frm=new  frmLogin();
        frm.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnThoatActionPerformed

    private void btnBatDauThiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBatDauThiActionPerformed
        // TODO add your handling code here:
        int index=tblDSDeThi.getSelectedRow();
        if(index>=0){
            frmThi frm=new frmThi();
            frm.setDETHI(dsDeThi.get(index));
            frm.setVisible(true);
            this.dispose();
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Vui lòng chọn đề thi", "Thông báo", JOptionPane.PLAIN_MESSAGE);
        }
    }//GEN-LAST:event_btnBatDauThiActionPerformed

    /**
     * @param args the command line arguments
     */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBatDauThi;
    private javax.swing.JButton btnThoat;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblDSDeThi;
    private javax.swing.JTable tblDeDaThi;
    // End of variables declaration//GEN-END:variables
}
